package com.baobao.web;

import com.baobao.dao.StudentDAO;

import com.baobao.domain.Student;

import com.baobao.service.StudentService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Student entities
 * 
 */

@Controller("StudentController")
public class StudentController {

	/**
	 * DAO injected by Spring that manages Student entities
	 * 
	 */
	@Autowired
	private StudentDAO studentDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Student entities
	 * 
	 */
	@Autowired
	private StudentService studentService;

	/**
	 * Select an existing Student entity
	 * 
	 */
	@RequestMapping("/selectStudent")
	public ModelAndView selectStudent(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(idKey));
		mav.setViewName("student/viewStudent.jsp");

		return mav;
	}

	/**
	 * Show all Student entities
	 * 
	 */
	@RequestMapping("/indexStudent")
	public ModelAndView listStudents() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("students", studentService.loadStudents());

		mav.setViewName("student/listStudents.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Student entities
	 * 
	 */
	public String indexStudent() {
		return "redirect:/indexStudent";
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Delete an existing Student entity
	 * 
	 */
	@RequestMapping("/deleteStudent")
	public String deleteStudent(@RequestParam String idKey) {
		Student student = studentDAO.findStudentByPrimaryKey(idKey);
		studentService.deleteStudent(student);
		return "forward:/indexStudent";
	}

	/**
	 * Create a new Student entity
	 * 
	 */
	@RequestMapping("/newStudent")
	public ModelAndView newStudent() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", new Student());
		mav.addObject("newFlag", true);
		mav.setViewName("student/editStudent.jsp");

		return mav;
	}

	/**
	 * Save an existing Student entity
	 * 
	 */
	@RequestMapping("/saveStudent")
	public String saveStudent(@ModelAttribute Student student) {
		studentService.saveStudent(student);
		return "forward:/indexStudent";
	}

	/**
	 * Edit an existing Student entity
	 * 
	 */
	@RequestMapping("/editStudent")
	public ModelAndView editStudent(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(idKey));
		mav.setViewName("student/editStudent.jsp");

		return mav;
	}

	/**
	 * Select the Student entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteStudent")
	public ModelAndView confirmDeleteStudent(@RequestParam String idKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("student", studentDAO.findStudentByPrimaryKey(idKey));
		mav.setViewName("student/deleteStudent.jsp");

		return mav;
	}

	/**
	 */
	@RequestMapping("/studentController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}
}